package com.ridaizu.dao.impl;

import static com.google.common.base.Preconditions.checkNotNull;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import com.ridaizu.dao.DaoAdapter;
import com.ridaizu.dao.db.DBAccess;
import com.ridaizu.model.Dictionary;
import com.ridaizu.model.Model;
import com.ridaizu.model.search.SearchModel;

@ManagedBean(name = "dictionaryDao")
@ApplicationScoped
public class DictionaryDao extends DaoAdapter {

	private static final long serialVersionUID = 1L;

	public int selectCount(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from m_dictionary \n");
		sql.append(searchModel.getSelectCountSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		if (rs == null) {
			return -1;
		}

		rs.next();

		return rs.getInt(1);
	}

	public List<Dictionary> selectSuitAgeList() throws SQLException {
		return selectDictionaryList(2);
	}

	public List<Dictionary> selectDamageLostList() throws SQLException {
		return selectDictionaryList(1);
	}

	public List<Dictionary> selectDictionaryList(int type) throws SQLException {

		String sql = "SELECT * FROM m_dictionary WHERE type = ? ORDER BY id ASC";
		ResultSet rs = DBAccess.query(sql.toString(), type);

		List<Dictionary> modelList = new ArrayList<Dictionary> ();
		while (rs.next()) {
			modelList.add((Dictionary)getModel(rs));
		}

		rs.close();

		return modelList;
	}

	public List<Dictionary> selectDictionaryList(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * FROM m_dictionary \n");
		sql.append(searchModel.getSelectSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		List<Dictionary> modelList = new ArrayList<Dictionary> ();
		while (rs.next()) {
			modelList.add((Dictionary)getModel(rs));
		}

		rs.close();

		return modelList;
	}

	@Override
	public Model getModel(ResultSet rs) throws SQLException {

		checkNotNull(rs);
		Dictionary dictionary = new Dictionary();
		dictionary.setId(rs.getInt("id"));
		dictionary.setType(rs.getInt("type"));
		dictionary.setName(rs.getString("name"));
		dictionary.setRgtOpt(rs.getString("rgt_opt"));
		dictionary.setRgtDate(rs.getTimestamp("rgt_date"));
		dictionary.setUpdOpt(rs.getString("upd_opt"));
		dictionary.setUpdDate(rs.getTimestamp("upd_date"));

		return dictionary;
	}

	@Override
	public int insert(Model model) {

		checkNotNull(model);

		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO m_dictionary \n");
		sql.append("( \n");
		sql.append("type, name, rgt_opt, \n");
		sql.append("rgt_date, upd_opt, upd_date \n");
		sql.append(") \n");
		sql.append("VALUES \n");
		sql.append("(?, ?, ?, ?, ?, ?); \n");

		Dictionary dictionary = (Dictionary) model;

		Object[] params = new Object[] {
				dictionary.getType(), dictionary.getName(), dictionary.getRgtOpt(),
				dictionary.getRgtDate(), dictionary.getUpdOpt(), dictionary.getUpdDate()
		};

		return DBAccess.update(sql.toString(), params);
	}

	@Override
	public int update(Model model) {

		checkNotNull(model);

		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE m_dictionary \n");
		sql.append("SET \n");
		sql.append("type = ?, \n");
		sql.append("name = ?, \n");
		sql.append("rgt_opt = ?, \n");
		sql.append("rgt_date = ?, \n");
		sql.append("upd_opt = ?, \n");
		sql.append("upd_date = ? \n");
		sql.append("WHERE id = ? \n");

		Dictionary dictionary = (Dictionary) model;

		Object[] params = new Object[] {
				dictionary.getType(), dictionary.getName(), dictionary.getRgtOpt(),
				dictionary.getRgtDate(), dictionary.getUpdOpt(), dictionary.getUpdDate(),
				dictionary.getId()
		};

		return DBAccess.update(sql.toString(), params);
	}

	public int delete(int id) {
		String sql = "DELETE FROM m_dictionary WHERE id=?;";

		return DBAccess.update(sql.toString(), id);
	}

}
